﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace 毕设
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.textBox1.Focus();
        }
        /// <summary>
        /// 输入框字符限制
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private void textBox_KeyPress(object sender, KeyPressEventArgs e)

        {   //限制只能输入数字，Backspace键，小数点

            if (!char.IsDigit(e.KeyChar) && e.KeyChar != '\b' && e.KeyChar != '.')

            {
                // !char.IsDigit(e.KeyChar) && e.KeyChar != '\b'&& e.KeyChar !='.'

                e.Handled = true;  //非以上键则禁止输入

            }

        }
        private void textBox_KeyPress4(object sender, KeyPressEventArgs e)

        {   //限制只能输入数字，Backspace键

            if (!char.IsDigit(e.KeyChar) && e.KeyChar != '\b')

            {
                e.Handled = true;  //非以上键则禁止输入

            }

        }
        /// <summary>
        /// 清空
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public void cleanAll()
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";
            textBox7.Text = "";
            textBox8.Text = "";
            textBox9.Text = "";
            textBox10.Text = "";
            textBox11.Text = "";
            textBox12.Text = "";
            textBox13.Text = "";
            textBox14.Text = "";
            textBox15.Text = "";
            textBox16.Text = "";
            textBox17.Text = "";
            textBox18.Text = "";
            textBox19.Text = "";
            textBox20.Text = "";
            label6.Text = "";
            label7.Text = "";
            label8.Text = "";
            label9.Text = "";
            label10.Text = "";
            label11.Text = "";
            label12.Text = "";
            label13.Text = "";
            label23.Text = "";
            label24.Text = "";
            label25.Text = "";
            label26.Text = "";
            label27.Text = "";
            label30.Text = "";
            label34.Text = "";
            label35.Text = "";
            label36.Text = "";
            label37.Text = "";
            label38.Text = "";
            label44.Text = "";
            label45.Text = "";
            label46.Text = "";
            label49.Text = "";
            label50.Text = "";
            label55.Text = "";
            label56.Text = "";
            label57.Text = "";

        }
        public void clean1()
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            label6.Text = "";
            label7.Text = "";
            label8.Text = "";
            label9.Text = "";
        }
        public void clean2()
        {
            textBox5.Text = "";
            textBox6.Text = "";
            textBox7.Text = "";
            textBox8.Text = "";
            textBox9.Text = "";
            label10.Text = "";
            label11.Text = "";
            label12.Text = "";
            label13.Text = "";
        }
        public void clean31()
        {
            textBox11.Text = "";
            textBox12.Text = "";
            textBox13.Text = "";
            textBox14.Text = "";
            label23.Text = "";
            label24.Text = "";
            label30.Text = "";
            label34.Text = "";
            label35.Text = "";
        }
        public void clean4()
        {
            textBox15.Text = "";
            textBox16.Text = "";
            textBox17.Text = "";
            textBox18.Text = "";
            textBox19.Text = "";
            label46.Text = "";
            label44.Text = "";
            label45.Text = "";

        }
        public void clean5()
        {
            textBox22.Text = "";
            textBox23.Text = "";
            textBox24.Text = "";
            textBox25.Text = "";
            textBox26.Text = "";
            label55.Text = "";
            label56.Text = "";
            label57.Text = "";

        }
        /// <summary>
        /// 转换为2进制
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static char twochar(int n)
        {

            if (n == 1) { return '1'; }
            else return '0';
        }
        public static string inttotwo(int value)
        {
            string temp = "";
            int shang = 1;
            int yushu;
            while (shang != 0)
            {
                shang = (int)value / 2;
                yushu = value % 2;
                value = shang;
                temp += twochar(yushu);
            }
            char[] temp3 = temp.ToCharArray();
            int l = temp3.Length - 1;
            //交换值
            for (int i = 0; i < l; i++, l--)
            {
                temp3[i] ^= temp3[l];
                temp3[l] ^= temp3[i];
                temp3[i] ^= temp3[l];
            }
            return new string(temp3);
        }
        /// <summary>
        /// 转换为16进制
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns
        public static char tochar(int n)
        {

            if (n == 10) { return 'a'; }
            else if (n == 1) { return '1'; }
            else if (n == 2) { return '2'; }
            else if (n == 3) { return '3'; }
            else if (n == 4) { return '4'; }
            else if (n == 5) { return '5'; }
            else if (n == 6) { return '6'; }
            else if (n == 7) { return '6'; }
            else if (n == 8) { return '8'; }
            else if (n == 9) { return '9'; }
            else if (n == 11) { return 'b'; }
            else if (n == 12) { return 'c'; }
            else if (n == 13) { return 'd'; }
            else if (n == 14) { return 'e'; }
            else if (n == 15) { return 'f'; }
            else return '0';
        }
        public static string inttohex(int value)
        {
            string temp = "";
            int shang = 1;
            int yushu;
            while (shang != 0)
            {
                shang = (int)value / 16;
                yushu = value % 16;
                value = shang;
                temp += tochar(yushu);
            }
            char[] temp3 = temp.ToCharArray();
            int l = temp3.Length - 1;
            //交换值
            for (int i = 0; i < l; i++, l--)
            {
                temp3[i] ^= temp3[l];
                temp3[l] ^= temp3[i];
                temp3[i] ^= temp3[l];
            }
            return new string(temp3);
        }
        /// <summary>
        /// 输入框长度限制
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (this.textBox1.Text.Length >= 4 || textBox1.Text.EndsWith("."))
            {
                string strNew = this.textBox1.Text.Substring(0, this.textBox1.Text.Length - 1);
                this.textBox1.Text = strNew;
                this.textBox1.SelectionStart = this.textBox1.Text.Length;
                this.textBox2.Focus();
            }
        }
        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            if (this.textBox2.Text.Length >= 4 || textBox2.Text.EndsWith("."))
            {
                string strNew = this.textBox2.Text.Substring(0, this.textBox2.Text.Length - 1);
                this.textBox2.Text = strNew;
                this.textBox2.SelectionStart = this.textBox2.Text.Length;
                this.textBox3.Focus();
            }
        }
        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            if (this.textBox3.Text.Length >= 4 || textBox3.Text.EndsWith("."))
            {
                string strNew = this.textBox3.Text.Substring(0, this.textBox3.Text.Length - 1);
                this.textBox3.Text = strNew;
                this.textBox3.SelectionStart = this.textBox3.Text.Length;
                this.textBox4.Focus();
            }
        }
        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            if (this.textBox5.Text.Length >= 4 || textBox5.Text.EndsWith("."))
            {
                string strNew = this.textBox5.Text.Substring(0, this.textBox5.Text.Length - 1);
                this.textBox5.Text = strNew;
                this.textBox5.SelectionStart = this.textBox5.Text.Length;
                this.textBox6.Focus();
            }
        }
        private void textBox6_TextChanged(object sender, EventArgs e)
        {
            if (this.textBox6.Text.Length >= 4 || textBox6.Text.EndsWith("."))
            {
                string strNew = this.textBox6.Text.Substring(0, this.textBox6.Text.Length - 1);
                this.textBox6.Text = strNew;
                this.textBox6.SelectionStart = this.textBox6.Text.Length;
                this.textBox7.Focus();
            }
        }
        private void textBox7_TextChanged(object sender, EventArgs e)
        {
            if (this.textBox7.Text.Length >= 4 || textBox7.Text.EndsWith("."))
            {
                string strNew = this.textBox7.Text.Substring(0, this.textBox7.Text.Length - 1);
                this.textBox7.Text = strNew;
                this.textBox7.SelectionStart = this.textBox7.Text.Length;
                this.textBox8.Focus();
            }
        }
        private void textBox8_TextChanged(object sender, EventArgs e)
        {
            if (this.textBox8.Text.Length >= 3)
            {
                this.textBox9.Focus();
            }
        }
        private void textBox11_TextChanged(object sender, EventArgs e)
        {
            if (this.textBox11.Text.Length >= 4 || textBox11.Text.EndsWith("."))
            {
                string strNew = this.textBox11.Text.Substring(0, this.textBox11.Text.Length - 1);
                this.textBox11.Text = strNew;
                this.textBox11.SelectionStart = this.textBox11.Text.Length;
                this.textBox12.Focus();
            }

        }
        private void textBox12_TextChanged(object sender, EventArgs e)
        {
            if (this.textBox12.Text.Length >= 4 || textBox12.Text.EndsWith("."))
            {
                string strNew = this.textBox12.Text.Substring(0, this.textBox12.Text.Length - 1);
                this.textBox12.Text = strNew;
                this.textBox12.SelectionStart = this.textBox12.Text.Length;
                this.textBox13.Focus();
            }
        }
        private void textBox13_TextChanged(object sender, EventArgs e)
        {
            if (this.textBox13.Text.Length >= 4 || textBox13.Text.EndsWith("."))
            {
                string strNew = this.textBox13.Text.Substring(0, this.textBox13.Text.Length - 1);
                this.textBox13.Text = strNew;
                this.textBox13.SelectionStart = this.textBox13.Text.Length;
                this.textBox14.Focus();
            }
        }
        private void textBox16_TextChanged(object sender, EventArgs e)
        {
            if (this.textBox16.Text.Length >= 4 || textBox16.Text.EndsWith("."))
            {
                string strNew = this.textBox16.Text.Substring(0, this.textBox16.Text.Length - 1);
                this.textBox16.Text = strNew;
                this.textBox16.SelectionStart = this.textBox16.Text.Length;
                this.textBox17.Focus();
            }
        }
        private void textBox17_TextChanged(object sender, EventArgs e)
        {
            if (this.textBox17.Text.Length >= 4 || textBox17.Text.EndsWith("."))
            {
                string strNew = this.textBox17.Text.Substring(0, this.textBox17.Text.Length - 1);
                this.textBox17.Text = strNew;
                this.textBox17.SelectionStart = this.textBox17.Text.Length;
                this.textBox18.Focus();
            }
        }
        private void textBox18_TextChanged(object sender, EventArgs e)
        {
            if (this.textBox18.Text.Length >= 4 || textBox18.Text.EndsWith("."))
            {
                string strNew = this.textBox18.Text.Substring(0, this.textBox18.Text.Length - 1);
                this.textBox18.Text = strNew;
                this.textBox18.SelectionStart = this.textBox18.Text.Length;
                this.textBox19.Focus();
            }
        }
        private void textBox19_TextChanged(object sender, EventArgs e)
        {
            if (this.textBox19.Text.Length >= 3)
            {

                this.textBox15.Focus();
            }
        }
        private void textBox22_TextChanged(object sender, EventArgs e)
        {
            if (this.textBox22.Text.Length >= 4 || textBox22.Text.EndsWith("."))
            {
                string strNew = this.textBox22.Text.Substring(0, this.textBox22.Text.Length - 1);
                this.textBox22.Text = strNew;
                this.textBox22.SelectionStart = this.textBox22.Text.Length;
                this.textBox23.Focus();
            }
        }
        private void textBox23_TextChanged(object sender, EventArgs e)
        {
            if (this.textBox23.Text.Length >= 4 || textBox23.Text.EndsWith("."))
            {
                string strNew = this.textBox23.Text.Substring(0, this.textBox23.Text.Length - 1);
                this.textBox23.Text = strNew;
                this.textBox23.SelectionStart = this.textBox23.Text.Length;
                this.textBox24.Focus();
            }
        }
        private void textBox24_TextChanged(object sender, EventArgs e)
        {
            if (this.textBox24.Text.Length >= 4 || textBox24.Text.EndsWith("."))
            {
                string strNew = this.textBox24.Text.Substring(0, this.textBox24.Text.Length - 1);
                this.textBox24.Text = strNew;
                this.textBox24.SelectionStart = this.textBox24.Text.Length;
                this.textBox25.Focus();
            }
        }
        private void textBox25_TextChanged(object sender, EventArgs e)
        {
            if (this.textBox25.Text.Length >= 3)
            {

                this.textBox26.Focus();
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            cleanAll();
            this.textBox1.Focus();
            //Form1.Font = new Font();
        }
        /// <summary>
        /// 界面1按钮
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private void Btn1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "")
            {
                MessageBox.Show("IP地址不能为空！！请重新输入");
                clean1();
            }
            else if (int.Parse(textBox1.Text) > 255 || int.Parse(textBox2.Text) > 255 || int.Parse(textBox3.Text) > 255 || !(int.Parse(textBox4.Text) <= 255))
            {
                MessageBox.Show("IP地址不合法！！请重新输入");
                clean1();

            }
            else
            {
                if (int.Parse(textBox1.Text) <= 126)
                {
                    if (int.Parse(textBox1.Text) == 10)
                    {
                        label6.Text = "A类地址中的私有地址";
                        label7.Text = "255.0.0.0";
                        label8.Text = textBox1.Text + ".0.0.0";
                        label9.Text = textBox1.Text + ".255.255.255";
                    }
                    else {
                        label6.Text = "A类地址";
                        label7.Text = "255.0.0.0";
                        label8.Text = textBox1.Text + ".0.0.0";
                        label9.Text = textBox1.Text + ".255.255.255"; }
                }
                else if (int.Parse(textBox1.Text) == 127)
                {
                    if (int.Parse(textBox1.Text) == 127 && int.Parse(textBox2.Text) == 0 && int.Parse(textBox3.Text) == 0 && int.Parse(textBox4.Text) == 1)
                    {
                        label6.Text = "本地回环地址！";
                        label7.Text = "";
                        label8.Text = "";
                        label9.Text = "";
                    }
                    else {
                    label6.Text = "保留地址！用做循环测试！";
                    label7.Text = "";
                    label8.Text =  "";
                    label9.Text =  ""; }
                }
                else if (int.Parse(textBox1.Text) <= 191)
                {
                    if (int.Parse(textBox1.Text) == 172 && int.Parse(textBox2.Text) >= 16 && int.Parse(textBox2.Text) <= 31)
                    {
                        label6.Text = "B类地址中的私有地址！";
                        label7.Text = "255.255.0.0";
                        label8.Text = textBox1.Text + "." + textBox2.Text + ".0.0";
                        label9.Text = textBox1.Text + "." + textBox2.Text + ".255.255";
                    }
                    else if (int.Parse(textBox1.Text) == 169 && int.Parse(textBox2.Text) == 254)
                    {
                        label6.Text = "B类地址中的保留地址！";
                        label7.Text = "255.255.0.0";
                        label8.Text = textBox1.Text + "." + textBox2.Text + ".0.0";
                        label9.Text = textBox1.Text + "." + textBox2.Text + ".255.255";
                    }
                    else {
                        label6.Text = "B类地址";
                        label7.Text = "255.255.0.0";
                        label8.Text = textBox1.Text + "." + textBox2.Text + ".0.0";
                        label9.Text = textBox1.Text + "." + textBox2.Text + ".255.255"; }
                }
                else if (int.Parse(textBox1.Text) <= 223)
                {
                    if(int.Parse(textBox1.Text) ==192 && int.Parse(textBox2.Text) ==168)
                    { 
                        label6.Text = "C类地址中的私有地址！";
                        label7.Text = "255.255.255.0";
                        label8.Text = textBox1.Text + "." + textBox2.Text + "." + textBox3.Text + ".0";
                        label9.Text = textBox1.Text + "." + textBox2.Text + "." + textBox3.Text + ".255";
                    }
                    else { 
                    label6.Text = "C类地址";
                    label7.Text = "255.255.255.0";
                    label8.Text = textBox1.Text + "." + textBox2.Text + "." + textBox3.Text + ".0";
                    label9.Text = textBox1.Text + "." + textBox2.Text + "." + textBox3.Text + ".255";}
                }
                else if (int.Parse(textBox1.Text) <= 239)
                {
                    label6.Text = "D类地址，用于多播";
                    label7.Text = "";
                    label8.Text = "";
                    label9.Text = "";
                }
                else
                {
                    label6.Text = "E类地址，保留";
                    label7.Text = "";
                    label8.Text = "";
                    label9.Text = "";
                }
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            clean1();


        }
        private void tabPage1_Click(object sender, EventArgs e)
        {
            this.textBox1.Focus();
            //clean();
        }
        /// <summary>
        /// 界面2按钮
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private void Btn2_Click(object sender, EventArgs e)
        {
            if (textBox5.Text == "" || textBox6.Text == "" || textBox7.Text == "" || textBox8.Text == "")
            {
                MessageBox.Show("IP地址不能为空！！请重新输入");
                clean2();
            }
            else if (int.Parse(textBox5.Text) > 255 || int.Parse(textBox6.Text) > 255 || int.Parse(textBox7.Text) > 255 || int.Parse(textBox8.Text) > 255)
            {
                MessageBox.Show("IP地址不合法！！请重新输入");
                clean2();

            }                 
            else if ( int.Parse(textBox9.Text)> 32 || textBox9.Text == "") { MessageBox.Show("前缀输入有误"); }
            else
            {
                int b = int.Parse(textBox9.Text);
                double a = Math.Pow(2, 32 - b) - 2;
                if(a>0){ label13.Text = a.ToString(); }
                else { label13.Text = "0"; }
                int c = int.Parse(textBox9.Text) / 8;
                //int d = int.Parse(textBox9.Text) % 8;
               // MessageBox.Show(d.ToString());
                if (c < 1)
                {
                    int d = int.Parse(textBox9.Text) % 8;
                    int ch = 0;
                    if (d == 1) { ch = int.Parse(textBox5.Text) &0x80 ; }
                    if (d == 2) { ch = int.Parse(textBox5.Text) & 0xc0; }
                    if (d == 3) { ch = int.Parse(textBox5.Text) & 0xe0; }
                    if (d == 4) { ch = int.Parse(textBox5.Text) & 0xf0; }
                    if (d == 5) { ch = int.Parse(textBox5.Text) & 0xf8; }
                    if (d == 6) { ch = int.Parse(textBox5.Text) & 0xfc; }
                    if (d == 7) { ch = int.Parse(textBox5.Text) & 0xfe; }
                    double s = 0;                    
                    for (int i = d; i > 0; i--)
                    {
                      s += Math.Pow(2, 8 - i);                                              
                    }
                    double k = 0;
                    k = (int)Math.Pow(2, 8 - d) - 1+ ch;
                   // k = Convert.ToString(k, 2);
                   // MessageBox.Show (System. Convert.ToString (k, 2));
                    label12.Text = s.ToString() + ".0.0.0";
                    label11.Text = ch.ToString() + ".0.0.0";
                    label10.Text = k.ToString() + ".255.255.255";
                }
                else if (c < 2)
                {
                    int d = int.Parse(textBox9.Text) % 8;
                    int ch = 0;
                    if (d == 1) { ch = int.Parse(textBox6.Text) & 0x80; }
                    if (d == 2) { ch = int.Parse(textBox6.Text) & 0xc0; }
                    if (d == 3) { ch = int.Parse(textBox6.Text) & 0xe0; }
                    if (d == 4) { ch = int.Parse(textBox6.Text) & 0xf0; }
                    if (d == 5) { ch = int.Parse(textBox6.Text) & 0xf8; }
                    if (d == 6) { ch = int.Parse(textBox6.Text) & 0xfc; }
                    if (d == 7) { ch = int.Parse(textBox6.Text) & 0xfe; }
                    double s = 0;
                    for (int i = d; i > 0; i--)
                    {
                        s += Math.Pow(2, 8 - i);
                    }
                    double k = 0;
                    k = (int)Math.Pow(2, 8 - d) - 1 + ch;
                    label12.Text = "255." + s.ToString() + ".0.0";
                    label11.Text = textBox5.Text +"." + ch.ToString() + ".0.0";
                    label10.Text = textBox5.Text + "." + k.ToString() + ".255.255";
                }
                else if (c < 3)
                {
                    int d = int.Parse(textBox9.Text) % 8;
                    int ch = 0;
                    if (d == 1) { ch = int.Parse(textBox7.Text) & 0x80; }
                    if (d == 2) { ch = int.Parse(textBox7.Text) & 0xc0; }
                    if (d == 3) { ch = int.Parse(textBox7.Text) & 0xe0; }
                    if (d == 4) { ch = int.Parse(textBox7.Text) & 0xf0; }
                    if (d == 5) { ch = int.Parse(textBox7.Text) & 0xf8; }
                    if (d == 6) { ch = int.Parse(textBox7.Text) & 0xfc; }
                    if (d == 7) { ch = int.Parse(textBox7.Text) & 0xfe; }
                    double s = 0;
                    for (int i = d; i > 0; i--)
                    {
                        s += Math.Pow(2, 8 - i);
                    }
                    double k = 0;
                    k = (int)Math.Pow(2, 8 - d) - 1 + ch;
                    label12.Text = "255.255." + s.ToString() + ".0";
                    label11.Text = textBox5.Text  + "."+textBox6.Text  + "." + ch.ToString() + ".0";
                    label10.Text = textBox5.Text + "." + textBox6.Text + "." + k.ToString() + ".255";
                }
                else if (c < 4)
                {
                    int d = int.Parse(textBox9.Text) % 8;
                    int ch = 0;
                    if (d == 1) { ch = int.Parse(textBox8.Text) & 0x80; }
                    if (d == 2) { ch = int.Parse(textBox8.Text) & 0xc0; }
                    if (d == 3) { ch = int.Parse(textBox8.Text) & 0xe0; }
                    if (d == 4) { ch = int.Parse(textBox8.Text) & 0xf0; }
                    if (d == 5) { ch = int.Parse(textBox8.Text) & 0xf8; }
                    if (d == 6) { ch = int.Parse(textBox8.Text) & 0xfc; }
                    if (d == 7) { ch = int.Parse(textBox8.Text) & 0xfe; }
                    double s = 0;
                    for (int i = int.Parse(textBox9.Text) - 24; i > 0; i--)
                    {
                        s += Math.Pow(2, 8 - i);
                    }
                    double k = 0;
                    k = (int)Math.Pow(2, 8 - d) - 1 + ch;
                    label12.Text = "255.255.255." + s.ToString();
                    label11.Text = textBox5.Text + "." + textBox6.Text  + "."+textBox7.Text  + "." + ch.ToString();
                    label10.Text = textBox5.Text + "." + textBox6.Text + "." + textBox7.Text + "." + k.ToString();
                }
                else
                {                   
                    label12.Text = "255.255.255.255";
                    label11.Text = textBox5.Text + "." + textBox6.Text + "." + textBox7.Text + "." + textBox8.Text;
                    label10.Text = textBox5.Text + "." + textBox6.Text + "." + textBox7.Text + "." + textBox8.Text;
                }
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            clean2();
        }
        private void tabPage2_Click(object sender, EventArgs e)
        {
            this.textBox5.Focus();
           // clean();
        }
        /// <summary>
        /// 界面3按钮
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private void button4_Click(object sender, EventArgs e)
        {
            if (textBox11.Text == "" || textBox12.Text == "" || textBox13.Text == "" || textBox14.Text == "")
            {
                MessageBox.Show("IP地址不能为空！！请重新输入");
                clean31();
            }
            else if (int.Parse(textBox11.Text) > 255 || int.Parse(textBox12.Text) > 255 || int.Parse(textBox13.Text) > 255 || int.Parse(textBox14.Text) > 255)
            {
                MessageBox.Show("IP地址不合法！！请重新输入");
                clean31();

            }
            else
            {
                label24.Text = inttotwo (int.Parse(textBox11.Text)).ToString() + ".";
                label30.Text = inttotwo(int.Parse(textBox12.Text)).ToString() + ".";
                label34.Text = inttotwo(int.Parse(textBox13.Text)).ToString() + ".";
                label35.Text = inttotwo(int.Parse(textBox14.Text)).ToString();
                label23.Text =inttohex(int.Parse(textBox11.Text)).ToString() + "."+ inttohex(int.Parse(textBox12.Text)).ToString() + "."+ inttohex(int.Parse(textBox13.Text)).ToString() + "."+ inttohex(int.Parse(textBox14.Text)).ToString();
            }
        }
        private void button6_Click(object sender, EventArgs e)
        {
            if (textBox10.Text == "" )
            {
                MessageBox.Show("前缀不能为空！！请重新输入！！");
                textBox10.Text = "";
            }
            else  if (int.Parse(textBox10.Text) > 32) { MessageBox.Show("前缀输入有误"); textBox10.Text = ""; }
            else
            {
                int c = int.Parse(textBox10.Text) / 8;
                if (c < 1)
                {
                    int d = int.Parse(textBox10.Text) % 8;
                    double s = 0;
                    for (int i = d; i > 0; i--)
                    {
                        s += Math.Pow(2, 8 - i);
                    }
                    label27.Text = s.ToString() + ".0.0.0";
                    label26.Text =inttotwo((int)s).ToString() + ".";
                    label36.Text = "0.";
                    label37.Text = "0.";
                    label38.Text = "0";
                    label25.Text =inttohex ((int)s).ToString() + ".0.0.0";
                }
                else if (c < 2)
                {
                    int d = int.Parse(textBox10.Text) % 8;
                    double s = 0;
                    for (int i = d; i > 0; i--)
                    {
                        s += Math.Pow(2, 8 - i);
                    }
                    label27.Text ="255."+ s.ToString() + ".0.0";
                    label26.Text = inttotwo(255).ToString() + ".";
                    label36 .Text = inttotwo((int)s).ToString() + ".";
                    label37.Text = "0.";
                    label38.Text = "0";
                    label25.Text = inttohex(255).ToString() +"."+ inttohex((int)s).ToString() + ".0.0";
                }
                else if (c < 3)
                {
                    int d = int.Parse(textBox10.Text) % 8;
                    double s = 0;
                    for (int i = d; i > 0; i--)
                    {
                        s += Math.Pow(2, 8 - i);
                    }
                    label27.Text ="255.255."+ s.ToString() + ".0";
                    label26.Text = inttotwo(255).ToString() + ".";
                    label36.Text = inttotwo(255).ToString() + ".";
                    label37.Text = inttotwo((int)s).ToString() + ".";
                    label38.Text = "0";
                    label25.Text = inttohex(255).ToString() + "."+inttohex(255).ToString() + "." + inttohex((int)s).ToString() + ".0";
                }
                else if (c < 4)
                {
                    int d = int.Parse(textBox10.Text) % 8;
                    double s = 0;
                    for (int i = d; i > 0; i--)
                    {
                        s += Math.Pow(2, 8 - i);
                    }
                    label27.Text ="255.255.255." + s.ToString() ;
                    label26.Text = inttotwo(255).ToString() + ".";
                    label36.Text = inttotwo(255).ToString() + ".";
                    label37.Text = inttotwo(255).ToString() + ".";
                    label38.Text = inttotwo((int)s).ToString() ;
                    label25.Text = inttohex(255).ToString() + "." + inttohex(255).ToString() + "." + inttohex(255).ToString() + "." + inttohex((int)s).ToString() ;
                }
                else
                {
                    label27.Text = "255.255.255.255";
                    label26.Text = inttotwo(255).ToString() + ".";
                    label36.Text = inttotwo(255).ToString() + ".";
                    label37.Text = inttotwo(255).ToString() + ".";
                    label38.Text = inttotwo(255).ToString() ;
                    label25.Text = inttohex(255).ToString() + "." + inttohex(255).ToString() + "." + inttohex(255).ToString() + "." + inttohex(255).ToString();
                }
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            clean31();        
        }
        private void button5_Click(object sender, EventArgs e)
        {
            textBox10.Text = "";
            label25.Text = "";
            label26.Text = "";
            label27.Text = "";
            label36.Text = "";
            label37.Text = "";
            label38.Text = "";
        }
        private void tabPage3_Click(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 界面4按钮
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private void button8_Click(object sender, EventArgs e)
        {
            if (textBox16.Text == "" || textBox17.Text == "" || textBox18.Text == "" || textBox19.Text == "" )
            {
                MessageBox.Show("IP地址不能为空！！请重新输入");
                clean4();
            }
            else if (int.Parse(textBox16.Text) > 255 || int.Parse(textBox17.Text) > 255 || int.Parse(textBox18.Text) > 255 || int.Parse(textBox19.Text) > 255)
            {
                MessageBox.Show("IP地址不合法！！请重新输入");
                clean4();

            }
            else if (int.Parse(textBox15.Text) > 32|| textBox15.Text == "")
            {
                MessageBox.Show("前缀不合法！！请重新输入");
                textBox15.Text = ""; 
            }
            else
            {

                int b = int.Parse(textBox15.Text);
                double a = Math.Pow(2, 32 - b) - 2;
                if (a > 0) { label44.Text = a.ToString(); }
                else { label44.Text = "0"; }
                int c = int.Parse(textBox15.Text) / 8;
                if (c < 1)
                {
                    int d = int.Parse(textBox15.Text) % 8;
                    int ch = 0;
                    if (d == 1) { ch = int.Parse(textBox16.Text) & 0x80; }
                    if (d == 2) { ch = int.Parse(textBox16.Text) & 0xc0; }
                    if (d == 3) { ch = int.Parse(textBox16.Text) & 0xe0; }
                    if (d == 4) { ch = int.Parse(textBox16.Text) & 0xf0; }
                    if (d == 5) { ch = int.Parse(textBox16.Text) & 0xf8; }
                    if (d == 6) { ch = int.Parse(textBox16.Text) & 0xfc; }
                    if (d == 7) { ch = int.Parse(textBox16.Text) & 0xfe; }
                    double k = 0;
                    k = (int)Math.Pow(2, 8 - d) - 1 + ch;
                    // k = Convert.ToString(k, 2);
                    // MessageBox.Show (System. Convert.ToString (k, 2));
                    label45.Text = ch.ToString() + ".0.0.1";
                    label46.Text = k.ToString() + ".255.255.254";
                }
                else  if (c < 2)
                {
                    int d = int.Parse(textBox15.Text) % 8;
                    int ch = 0;
                    if (d == 1) { ch = int.Parse(textBox17.Text) & 0x80; }
                    if (d == 2) { ch = int.Parse(textBox17.Text) & 0xc0; }
                    if (d == 3) { ch = int.Parse(textBox17.Text) & 0xe0; }
                    if (d == 4) { ch = int.Parse(textBox17.Text) & 0xf0; }
                    if (d == 5) { ch = int.Parse(textBox17.Text) & 0xf8; }
                    if (d == 6) { ch = int.Parse(textBox17.Text) & 0xfc; }
                    if (d == 7) { ch = int.Parse(textBox17.Text) & 0xfe; }
                    double k = 0;
                    k = (int)Math.Pow(2, 8 - d) - 1 + ch;
                    label45.Text = textBox16.Text + "." + ch.ToString() + ".0.1";
                    label46.Text = textBox16.Text + "." + k.ToString() + ".255.254";
                }
                else if (c < 3)
                {
                    int d = int.Parse(textBox15.Text) % 8;
                    int ch = 0;
                    if (d == 1) { ch = int.Parse(textBox18.Text) & 0x80; }
                    if (d == 2) { ch = int.Parse(textBox18.Text) & 0xc0; }
                    if (d == 3) { ch = int.Parse(textBox18.Text) & 0xe0; }
                    if (d == 4) { ch = int.Parse(textBox18.Text) & 0xf0; }
                    if (d == 5) { ch = int.Parse(textBox18.Text) & 0xf8; }
                    if (d == 6) { ch = int.Parse(textBox18.Text) & 0xfc; }
                    if (d == 7) { ch = int.Parse(textBox18.Text) & 0xfe; }
                    double k = 0;
                    k = (int)Math.Pow(2, 8 - d) - 1 + ch;
                    label45.Text = textBox16.Text + "." + textBox17.Text + "." + ch.ToString() + ".1";
                    label46.Text = textBox16.Text + "." + textBox17.Text + "." + k.ToString() + ".254";
                }
                else 
                {
                    int d = int.Parse(textBox15.Text) % 8;
                    int ch = 0;
                    if (d == 1) { ch = int.Parse(textBox19.Text) & 0x80; }
                    if (d == 2) { ch = int.Parse(textBox19.Text) & 0xc0; }
                    if (d == 3) { ch = int.Parse(textBox19.Text) & 0xe0; }
                    if (d == 4) { ch = int.Parse(textBox19.Text) & 0xf0; }
                    if (d == 5) { ch = int.Parse(textBox19.Text) & 0xf8; }
                    if (d == 6) { ch = int.Parse(textBox19.Text) & 0xfc; }
                    if (d == 7) { ch = int.Parse(textBox19.Text) & 0xfe; }
                    double k = 0;
                    double s = ch + 1;
                    k = (int)Math.Pow(2, 8 - d) - 2 + ch;
                    if (k - s > 0)
                    {
                        label45.Text = textBox16.Text + "." + textBox17.Text + "." + textBox18.Text + "." + s.ToString();
                        label46.Text = textBox16.Text + "." + textBox17.Text + "." + textBox18.Text + "." + k.ToString();
                    }
                    else { label45.Text ="" ; label46.Text ="" ; }
                }
            } 
        }
        private void button9_Click(object sender, EventArgs e)
        {
            if (Int64 .Parse(textBox20.Text) > 0 && Int64  .Parse(textBox20.Text) < Math.Pow(2, 32)-1)
            {
                double a = Math.Log(Int64 .Parse(textBox20.Text) + 2, 2);//取对数
                double b = Math.Ceiling(a);//上取整
                //double c = 0;
                label50.Text = (Math.Pow(2, b) - 2).ToString();
                if (b < 8)
                {
                    label49.Text = "255.255.255." + (255 - Math.Pow(2, b) + 1).ToString();
                }
                else if (b < 16)
                {
                    label49.Text = "255.255." + (255 - Math.Pow(2, b - 8) + 1).ToString() + ".0";
                }
                else if (b < 24)
                {
                    label49.Text = "255." + (255 - Math.Pow(2, b - 16) + 1).ToString() + ".0.0";
                }
                else
                {
                    label49.Text = (255 - Math.Pow(2, b - 24) + 1).ToString() + ".0.0.0";
                }
            }
            else { MessageBox.Show("地址需求数目有错！！！！！！！");}

        }
        private void button7_Click(object sender, EventArgs e)
        {
            clean4();
        }
        private void button10_Click(object sender, EventArgs e)
        {
            textBox20.Text = "";
            label49.Text = "";
            label50.Text = "";
        }
        /// <summary>
        /// 界面5按钮
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private void button12_Click(object sender, EventArgs e)
        {
            if (textBox22.Text == "" || textBox23.Text == "" || textBox24.Text == "" || textBox25.Text == "")
            {
                MessageBox.Show("IP地址不能为空！！请重新输入");
                clean5();
            }
            else if (int.Parse(textBox22.Text) > 255 || int.Parse(textBox23.Text) > 255 || int.Parse(textBox24.Text) > 255 || int.Parse(textBox25.Text) > 255)
            {
                MessageBox.Show("IP地址不合法！！请重新输入");
                clean5();

            }
            else
            {
                if (int.Parse(textBox22.Text) <= 126)
                {
                    int m = int.Parse(textBox26.Text);
                    if (m == 1)
                    {
                        m = 2;
                    }
                    double a = Math.Log(m, 2);//取对数
                    double b = Math.Ceiling(a);//上取整
                    if (b > 30 - 8)
                    {
                        MessageBox.Show("子网数不合法！！请重新输入");
                        clean5();
                    }
                    else
                    {                       
                        label55.Text = b.ToString();
                        int c = (int)b / 8;
                        if (c < 1)
                        {
                            int d = (int)b % 8;
                            //MessageBox.Show(d.ToString());
                            int  s = 0;
                            for (int i = d; i > 0; i--)
                            {
                                s += (int)Math.Pow(2, 8 - i);
                            }
                            //MessageBox.Show(s.ToString());
                            label56.Text = "255." + s.ToString() + ".0.0";
                            label57.Text = (Math.Pow(2, 24 - b) - 2).ToString();
                        }
                        else if (c < 2)
                        {
                            int d = (int)b % 8;
                            double s = 0;
                            for (int i = d; i > 0; i--)
                            {
                                s += Math.Pow(2, 8 - i);
                            }
                            label56.Text = "255.255." + s.ToString() + ".0";
                            label57.Text = (Math.Pow(2, 16 - b) - 2).ToString();
                        }
                        else if (c < 3)
                        {
                            int d = (int)b % 8;
                            double s = 0;
                            for (int i = d; i > 0; i--)
                            {
                                s += Math.Pow(2, 8 - i);
                            }
                            label56.Text = "255.255.255." + s.ToString();
                            label57.Text = (Math.Pow(2, 8 - b) - 2).ToString();
                        }
                        else { MessageBox.Show("子网数错误"); clean5(); }
                    }
                }
                else if (int.Parse(textBox22.Text) == 127) { MessageBox.Show("不进行子网划分");clean5(); }
                else if (int.Parse(textBox22.Text) <= 191)
                {
                    int m = int.Parse(textBox26.Text);
                    if (m == 1)
                    {
                        m = 2;
                    }
                    double a = Math.Log(m, 2);//取对数
                    double b = Math.Ceiling(a);//上取整
                    if (b > 30 - 16)
                    {
                        MessageBox.Show("子网数不合法！！请重新输入");
                        clean5();
                    }
                    else
                    {
                        label55.Text = b.ToString();
                        int c = (int)b / 8;
                        if (c < 1)
                        {
                            int d = (int)b % 8;
                            double s = 0;
                            for (int i = d; i > 0; i--)
                            {
                                s += Math.Pow(2, 8 - i);
                            }
                            label56.Text = "255.255." + s.ToString() + ".0";
                            label57.Text = (Math.Pow(2, 16 - b) - 2).ToString();
                        }
                        else if (c < 2)
                        {
                            int d = (int)b % 8;
                            double s = 0;
                            for (int i = d; i > 0; i--)
                            {
                                s += Math.Pow(2, 8 - i);
                            }
                            label56.Text = "255.255.255." + s.ToString();
                            label57.Text = (Math.Pow(2, 8 - b) - 2).ToString();
                        }

                        else { MessageBox.Show("子网数错误"); clean5(); }
                    }
                }
                else if (int.Parse(textBox22.Text) <= 223)
                {
                    int m = int.Parse(textBox26.Text);
                    if (m == 1)
                    {
                        m = 2;
                    }
                    double a = Math.Log(m, 2);//取对数
                    double b = Math.Ceiling(a);//上取整
                    if (b > 30 - 24)
                    {
                        MessageBox.Show("子网数不合法！！请重新输入");
                        clean5();
                    }
                    else
                    {                      
                        label55.Text = b.ToString();
                        int c = (int)b / 8;                  
                         if (c < 1)
                        {
                            int d = (int)b % 8;
                            double s = 0;
                            for (int i = d; i > 0; i--)
                            {
                                s += Math.Pow(2, 8 - i);
                            }
                            label56.Text = "255.255.255." + s.ToString();
                            label57.Text = (Math.Pow(2, 8 - b) - 2).ToString();
                        }

                        else { MessageBox.Show("子网数错误"); clean5(); }
                    }
                }
                else { MessageBox.Show("不进行子网划分");clean5(); }
            }
        }

        private void button11_Click(object sender, EventArgs e)
        {
            clean5();
        }
        /// <summary>
        /// 界面6按钮
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private void button13_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("https://www.ip5.me/");
        }
        private void button14_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://ip.zxinc.org/ipquery/");
        }
    }
}